/**
 * 动画对象 容器
 */
class AnimationSprite
    extends egret.Sprite {

    /**
     * 编译顺序 标识
     */
    public static referenceFlag: string = KeyBoardManager.referenceFlag;

    /**
     * 播放 动画 类型
     */
    public animationType: string = "";

    /**
     * 动画 类型
     */
    public static WALK: string = "walk"; // 步走 动画
    public static STANDING: string = "standing"; // 待机 动画
    public static FIRE: string = "fire"; // 攻击 动画

    /**
     * MovieClip 对象 相关参数
     */
    private data: any = null;
    private txtr: any = null;
    private mcFactory: egret.MovieClipDataFactory = null;
    private mc: egret.MovieClip = null;

    private frameLabel: string = ""; // 帧标签
    private firstFlag: boolean = true; // 首次 播放 标识

    /**
     * 构造函数
     */
    public constructor() {
        super();
    }

    /**
     * 初始化 函数
     * @param type
     */
    public init(type: string): void {

        let selectType: string = type; // 初始化 类型
        this.animationType = selectType;

        let jsonName: string = "";
        let imgName: string = "";
        let movieClipName: string = "";

        switch (selectType) {
            case AnimationSprite.FIRE:
                jsonName = "fire_json";
                imgName = "fire_png";
                movieClipName = "cat_a5";
                this.frameLabel = "fire";
                break;
            case AnimationSprite.WALK:
                jsonName = "walk_json";
                imgName = "walk_png";
                movieClipName = "cat_walk";
                this.frameLabel = "walk";
                break;
            case AnimationSprite.STANDING:
                jsonName = "standing_json";
                imgName = "standing_png";
                movieClipName = "cat_idle";
                this.frameLabel = "standing";
                break;
            default:
                break;
        }

        /**
         * 初始化 相关参数
         */
        this.data = RES.getRes(jsonName);
        this.txtr = RES.getRes(imgName);
        let dataTemp: any = this.data;
        let txtrTemp: any = this.txtr;

        this.mcFactory = new egret.MovieClipDataFactory(dataTemp, txtrTemp);
        let mcFactoryTemp: egret.MovieClipDataFactory = this.mcFactory;

        this.mc = new egret.MovieClip(mcFactoryTemp.generateMovieClipData(movieClipName));
        this.addChild(this.mc); // 添加到 容器
    }

    /**
     * 设置 容器对象 是否 可见
     * @param bool
     */
    public setVisible(bool: boolean): void {

        let isVisible: boolean = bool;
        this.visible = isVisible;
    }

    /**
     * 播放 动画
     */
    public play(): void {

        this.setVisible(true); // 该 容器对象 设置 可见

        if (this.mc.isPlaying == true) { // 避免 重复 执行 播放
            return;
        }

        if (this.firstFlag == true) { // 第一次 播放 动画

            let frame: string = this.frameLabel;
            this.mc.gotoAndPlay(frame, -1);
            this.firstFlag = false;

        } else { // 继续 播放 动画
            this.mc.play();
        }
    }

    /**
     * 暂停 动画
     */
    public stop(): void {

        if (this.mc.isPlaying == false) { // 避免 重复 执行 暂停
            return;
        }

        this.mc.stop();
        this.setVisible(false); // 该 容器对象 设置 不可见
    }


}